package com.butuh.uang.tuh.ang.util;

import android.content.Context;

import com.butuh.uang.tuh.ang.App;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import cn.droidlover.xdroidmvp.log.XLog;
import okhttp3.RequestBody;

public class JsonUtils {


    public static RequestBody mapToBody(HashMap<String, String> paramsMap) {
        StringBuffer sb = new StringBuffer();
        Iterator iterator = paramsMap.entrySet().iterator();
        for (int i = 0; i < paramsMap.size(); i++) {
            Map.Entry entry = (Map.Entry) iterator.next();
            String key = (String) entry.getKey();
            Object value = entry.getValue();
            if (i == 0) {
                sb.append("[");
            }
            sb.append("[" + "\"" + key + "\"" + "," + "\"" + value + "\"" + "]");
            if (i != paramsMap.size() - 1) {
                sb.append(",");
            }
            if (i == paramsMap.size() - 1) {
                sb.append("]");
            }
        }
        XLog.e("===sb===" + sb.toString());
        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json;charset=UTF-8"), sb.toString());
        return body;
    }


    public static String  getRequestBody(List<String> mList){
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < mList.size(); i++) {
            if (i == 0) {
                sb.append("[");
            }
            sb.append("\\\"" + mList.get(i) + "\\\"");

            if(i!=mList.size()-1){
                sb.append(",");
            }else{
                sb.append("]");
            }
        }
        return sb.toString();
    }


    public static String mapToJson(HashMap<String, String> paramsMap) {
        StringBuffer sb = new StringBuffer();
        Iterator iterator = paramsMap.entrySet().iterator();
        for (int i = 0; i < paramsMap.size(); i++) {
            Map.Entry entry = (Map.Entry) iterator.next();
            String key = (String) entry.getKey();
            Object value = entry.getValue();
            if (i == 0) {
                sb.append("[");
            }
            sb.append("[" + "\"" + key + "\"" + "," + "\"" + value + "\"" + "]");
            if (i != paramsMap.size() - 1) {
                sb.append(",");
            }
            if (i == paramsMap.size() - 1) {
                sb.append("]");
            }
        }
        return sb.toString();
    }


    public static RequestBody strToBody(String strEntity) {
        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json;charset=UTF-8"), strEntity);
        return body;
    }


    public static Map<String, String> getAllParmarts(Context context) {
        String uuid1 = App.getUUID1();
        String uuid2 = RandomUtil.getUUID(context);
        String data = String.valueOf(System.currentTimeMillis());
        String rand = RandomUtil.getRand();
        String id = RandomUtil.base64encodeToString(context).trim().replaceAll(" ", "");
        List<String> list = new ArrayList<>();
        list.add(uuid1);
        list.add(uuid2);
        list.add(data);
        list.add(rand);
        list.add(id);
        Collections.sort(list);
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < list.size(); i++) {
            sb.append(list.get(i));
        }

        String check = RandomUtil.encryptToSHA(sb.toString().trim());
        Map<String, String> map = new HashMap<>();
        map.put("Content-Type", "application/json");
        map.put("UUID1", uuid1);
        map.put("UUID2", uuid2);
        map.put("DATE", data);
        map.put("RAND", rand);
        map.put("CHECK", check);
        map.put("ANDROID", id);
        return map;
    }

}
